package com.deri.egov.vor;

import java.util.ArrayList;

import org.deri.vor.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;

public class POISearchActivity extends Activity {
	/** Called when the activity is first created. */
	private String radius = "500"; // default radius choice of the user
	private EditText fSaved_text;
	private Location cur_location = null;
	private LocationManager cur_lm = null;
	private LocationListener cur_locationListener = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.tag_main);
		start_current_location_listener(); // start listening to user's current
		// gps location
		fSaved_text = (EditText) findViewById(R.id.search_keyword);
		Button search_places_button = (Button) findViewById(R.id.ButtonSearchPlaces);
		Spinner spinner = (Spinner) findViewById(R.id.RadiusPrompt);
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
				this, R.array.radius_array,
				android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner.setAdapter(adapter);
		spinner.setSelection(4);
		spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
		search_places_button.setOnClickListener(search_placesClickListener);

		share_button();

	}

	public boolean isOnline() {
		ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

		return cm.getActiveNetworkInfo() != null
				&& cm.getActiveNetworkInfo().isConnectedOrConnecting();
	}

	public class MyOnItemSelectedListener implements OnItemSelectedListener {

		@Override
		public void onItemSelected(AdapterView<?> parent, View view, int pos,
				long id) {
			radius = parent.getItemAtPosition(pos).toString();
		}

		@Override
		public void onNothingSelected(AdapterView parent) {
			// Do nothing.
		}
	}

	private OnClickListener search_placesClickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {

			String  keywordtext=fSaved_text.getText().toString();
			if (!isOnline()) {

				AlertDialog.Builder dialog = new AlertDialog.Builder(
						POISearchActivity.this);
				dialog.setTitle("Alert");
				dialog.setMessage("You should turn on your internet connection");
				dialog.show();
			} 
			if(keywordtext.equals("")){
				AlertDialog.Builder dialog = new AlertDialog.Builder(
						POISearchActivity.this);
				dialog.setTitle("Alert");
				dialog.setMessage("Please enter a keyword");
				dialog.show();
			}
			else {
				if (cur_location == null) {
					Criteria crit = new Criteria();
					crit.setAccuracy(Criteria.ACCURACY_FINE);
					String provider = cur_lm.getBestProvider(crit, true);
					cur_location = cur_lm.getLastKnownLocation(provider);
					// Toast.makeText(getBaseContext(),Double.toString(cur_location.getLongitude()),
					// Toast.LENGTH_LONG).show();
				}
				String lat_str, lon_str;
				lon_str = Double.toString(cur_location.getLongitude());// -9.081441
				lat_str = Double.toString(cur_location.getLatitude());// "53.2912654";

				Intent myIntent = new Intent(POISearchActivity.this,
						GMapsActivity.class);
				ArrayList<String> value = new ArrayList<String>();
				value.add(0, lat_str);
				value.add(1, lon_str);
				value.add(2, radius.replace(" Meters", ""));
				value.add(3, keywordtext);

				// String pois="pp";
				myIntent.putStringArrayListExtra("data", value);

				POISearchActivity.this.startActivity(myIntent);

				finish();
			}
		}
	};

	private void start_current_location_listener() {

		long minTimeMillis = 0;
		long minDistanceMeters = 100;
		// set the location listener
		cur_lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

		cur_locationListener = new CurLocationListener();

		cur_lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,
				minTimeMillis, minDistanceMeters, cur_locationListener);

	}

	public class CurLocationListener implements LocationListener {

		@Override
		public void onLocationChanged(Location loc) {
			cur_location = loc;

		}

		@Override
		public void onProviderDisabled(String provider) {

		}

		@Override
		public void onProviderEnabled(String provider) {

		}

		@Override
		public void onStatusChanged(String provider, int status, Bundle extras) {

		}

	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	@Override
	protected void onStop() {
		super.onStop();
		cur_lm.removeUpdates(cur_locationListener);

	}

	private void share_button() {
		Button button = (Button) findViewById(R.id.btnBack);
		button.setOnClickListener(ms_layout);

		// About Button OnClick
		button = (Button) findViewById(R.id.btnAbout);
		button.setOnClickListener(ms_layout);

		// Home Button OnClick
		button = (Button) findViewById(R.id.btnHome);
		button.setOnClickListener(ms_layout);

		ImageView im = (ImageView) findViewById(R.id.vor_icon);
		im.setClickable(true);
		im.setOnClickListener(ms_layout);

		im = (ImageView) findViewById(R.id.nuig_icon);
		im.setClickable(true);
		im.setOnClickListener(ms_layout);

		im = (ImageView) findViewById(R.id.deri_icon);
		im.setClickable(true);
		im.setOnClickListener(ms_layout);
	}

	private OnClickListener ms_layout = new OnClickListener() {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.btnAbout: {
				jump(72);
				break;
			}
			case R.id.btnHome: {
				jump(2);
				break;
			}
			case R.id.btnBack: {
				jump(2);
				break;
			}
			case R.id.vor_icon: {
				jump(3);
				break;
			}
			case R.id.deri_icon: {
				jump(19);
				break;
			}
			case R.id.nuig_icon: {
				jump(40);
				break;
			}
			}
		}
	};

	private void jump(int page) {
		Intent i = new Intent();
		i.putExtra("cl", page);
		i.setClassName("org.deri.vor", "org.deri.vor.VovolOceanRaceActivity");
		startActivity(i);
		finish();
	}

}
